package com.hsy.order.order.action

import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import com.flux.action.SubscriptionManager
import com.flux.dispatcher.Dispatcher
import com.hsy.order.base.action.ActionType
import com.hsy.order.base.action.BaseActionCreator
import com.hsy.order.base.action.Key
import com.hsy.order.base.http.RetrofitFactory

/**
 * 首页行为生成器
 * Created on 2018/3/26.
 *
 * @author Yang
 */
class HomeActionCreator(dispatcher: Dispatcher, manager: SubscriptionManager) : BaseActionCreator(dispatcher, manager) {

    private val service = RetrofitFactory.getService(HomeService::class.java)

    /**
     * 获取首页数据
     */
    fun getHomeData(type: String, page: String, tab: String) {
        val action = newAction(ActionType.GET_HOME_DATA + tab)

        val params = HomeParam(type, page)
        service.getHomeData(params.toMap())
                .subscribeOn(Schedulers.io())
                .filter { response -> checkResult(response.error, "0", response.msg) }
                .map { response -> response.result }
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ homeData ->
                    action.data[Key.HOME_DATA] = homeData
                    postRxAction(action)
                }, { throwable ->
                    handleHttpError(throwable)
                    action.success = false
                    postRxAction(action)
                })
    }

    /**
     * 接受订单
     * @param id 订单ID
     */
    fun acceptOrder(id: String) {
        val action = newAction(ActionType.ACCEPT_ORDER)

        val params = OrderParam(id)
        service.acceptOrder(params.toMap())
                .subscribeOn(Schedulers.io())
                .filter { response -> checkResult(response.error, "0", response.msg) }
                .map { response -> response.result }
                .filter { result -> checkResult(result.done, "1", result.msg) }
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ homeData ->
                    action.data[Key.ORDER_RESULT] = homeData
                    postRxAction(action)
                }, { throwable ->
                    handleHttpError(throwable)
                    action.success = false
                    postRxAction(action)
                })
    }

    /**
     * 拒绝订单
     * @param id 订单ID
     */
    fun refuseOrder(id: String) {
        val action = newAction(ActionType.REFUSED_ORDER)

        val params = OrderParam(id)
        service.refuseOrder(params.toMap())
                .subscribeOn(Schedulers.io())
                .filter { response -> checkResult(response.error, "0", response.msg) }
                .map { response -> response.result }
                .filter { result -> checkResult(result.done, "1", result.msg) }
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ homeData ->
                    action.data[Key.ORDER_RESULT] = homeData
                    postRxAction(action)
                }, { throwable ->
                    handleHttpError(throwable)
                    action.success = false
                    postRxAction(action)
                })
    }

    /**
     * 派送订单
     * @param id 订单ID
     */
    fun dispatchOrder(id: String) {
        val action = newAction(ActionType.DISPATCH_ORDER)

        val params = OrderParam(id)
        service.dispatchOrder(params.toMap())
                .subscribeOn(Schedulers.io())
                .filter { response -> checkResult(response.error, "0", response.msg) }
                .map { response -> response.result }
                .filter { result -> checkResult(result.done, "1", result.msg) }
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ homeData ->
                    action.data[Key.ORDER_RESULT] = homeData
                    postRxAction(action)
                }, { throwable ->
                    handleHttpError(throwable)
                    action.success = false
                    postRxAction(action)
                })
    }
}